# -*- coding: utf-8 -*-

import cookielib
import urllib2
import urllib
import httplib
import urlparse
import time

import jsonHander

'''网络数据查询实现类，负责网络数据交互，包括登陆和JSON数据获取'''
class QueryCore():
    def __init__(self, ctr):
        self.ctr = ctr
        self.setCookies()

    """ 设置cookie"""
    def setCookies(self):   
        self.cookieFile = "cookies.txt"
        self.cookies = cookielib.MozillaCookieJar(self.cookieFile)
        
        try:
            self.cookies.load(ignore_discard = True, ignore_expires = True)
        except Exception:
            self.saveCookie()

        self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cookies))
        urllib2.install_opener(self.opener)

        try:
            response = self.opener.open(urllib2.Request('https://app.figure1.com'))
            response.read()
        except:
            pass
        self.saveCookie()

    """保存cookie"""
    def saveCookie(self):
        self.cookies.save(self.cookieFile, ignore_discard = True, ignore_expires = True)

    """获得全部cookie数据"""
    def readAllCookie(self):
        cookieMap = {}
        if self.cookies is not None:
            for cookie in self.cookies:
                one = cookie.name + '=' + str(cookie.value) + ('; expires=' + str(cookie.expires) if cookie.expires is not None else '' ) + '; domain=' + str(cookie.domain) + '; path=' + str(cookie.path)
                cookieMap.update({cookie.name: one})
        return cookieMap

    """更新cookie"""
    def updateCookie(self):
        self.saveCookie()
        self.cookies.load(ignore_discard = True, ignore_expires = True)
        self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cookies))
        urllib2.install_opener(self.opener)
        
    '''异步登陆Figure1，实际上直接用login就行了'''
    def loginAync(self, postData):
        def run(self, postData):
            try:
                url = 'https://app.figure1.com/login'
                req = urllib2.Request(url, urllib.urlencode(postData))
                
                req.add_header('Host', 'app.figure1.com')
                req.add_header('User-Agent', 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36')
                req.add_header('Referer', 'https://app.figure1.com/login')
                req.add_header('Connection', 'keep-alive')
                req.add_header('X-Requested-With', 'XMLHttpRequest')
                req.add_header('Content-Type', 'application/x-www-form-urlencoded')
                
                response = urllib2.urlopen(req)
                #print response.read()
                
                return {'s': True, 'r': response.read()}
            except:
                print '登陆失败，尝试再次登陆中---'
                return {'s': False, 'r': ''}

        re = run(self, postData)

        while not re['s']:
            re = run(self, postData)

        ''' fix-me! 这里还要对返回的re['r']做处理，判断是否登陆成功，由于本程序有特定用途，这里就不做太多判断了。'''
        return True

    """用户登陆，返回登陆状态码"""
    def login(self, loginData):
        if self.loginAync(loginData):
            try:
                '''
                url = "https://app.figure1.com/login"
                req = urllib2.Request(url, urllib.urlencode(loginData))
                
                req.add_header('Host', 'app.figure1.com')
                req.add_header('User-Agent', 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36')
                req.add_header('Referer', 'https://app.figure1.com/login')
                req.add_header('Connection', 'keep-alive')
                req.add_header('X-Requested-With', 'XMLHttpRequest')
                req.add_header('Content-Type', 'application/x-www-form-urlencoded')
                
                response = urllib2.urlopen(req)
                '''
            except:
                return False

            return True
        else:
            return False
        
    '''获取Figure1分类JSON'''
    def getCategoryData(self):
        def run(self):
            try:
                url_anatomy = 'https://app.figure1.com/s/feeds/category/anatomy'
                url_specialty = 'https://app.figure1.com/s/feeds/category/specialty'
				
				
                
                req_anatomy = urllib2.Request(url_anatomy)
                req_anatomy.add_header('Host', 'app.figure1.com')
                req_anatomy.add_header('User-Agent', 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36')
                req_anatomy.add_header('Referer', 'https://app.figure1.com/login')
                req_anatomy.add_header('Connection', 'keep-alive')
                req_anatomy.add_header('X-Requested-With', 'XMLHttpRequest')
                req_anatomy.add_header('Content-Type', 'application/x-www-form-urlencoded')
                
                req_specialty = urllib2.Request(url_specialty)
                req_specialty.add_header('Host', 'app.figure1.com')
                req_specialty.add_header('User-Agent', 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36')
                req_specialty.add_header('Referer', 'https://app.figure1.com/login')
                req_specialty.add_header('Connection', 'keep-alive')
                req_specialty.add_header('X-Requested-With', 'XMLHttpRequest')
                req_specialty.add_header('Content-Type', 'application/x-www-form-urlencoded')

                print url_anatomy
				
                json_anatomy = urllib2.urlopen(req_anatomy).read()
				
                print url_specialty
				
                json_specialty = urllib2.urlopen(req_specialty).read()
                
                jsonMerged = jsonHander.mergeJson(json_anatomy, json_specialty)
                
                print jsonMerged
                
                return {'s': True, 'r': jsonMerged}
            except:
                print '获取分类信息错误，重试中---'
                return {'s': False, 'r': ''}

        re = run(self)

        while not re['s']:
            re = run(self)

        return re['r']
    
    '''获取Figure1分类图片信息JSON'''
    def getCategoryDisplaySrc(self, category_id):
        def run(self):
            try:
                url = 'https://app.figure1.com/s/image/category/' + category_id
                req = urllib2.Request(url)
                
                req.add_header('Host', 'app.figure1.com')
                req.add_header('User-Agent', 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36')
                req.add_header('Referer', 'https://app.figure1.com/login')
                req.add_header('Connection', 'keep-alive')
                req.add_header('X-Requested-With', 'XMLHttpRequest')
                req.add_header('Content-Type', 'application/x-www-form-urlencoded')
                
                response = urllib2.urlopen(req)
                
                return {'s': True, 'r': response.read()}
            except:
                print '获取分类图片地址JSON错误，重试中---'
                return {'s': False, 'r': ''}

        re = run(self)

        while not re['s']:
            re = run(self)
            
        return jsonHander.getCategoryDisplaySrc(re['r'])
    
    
    '''保存分类图片'''
    def saveCategoryImage(self, category_name, image_src):
        category_image = ''
        while True:
            url = 'https://app.figure1.com' + image_src
            req = urllib2.Request(url)
            
            print url
            
            req.add_header('Host', 'app.figure1.com')
            req.add_header('User-Agent', 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36')
            req.add_header('Referer', 'https://app.figure1.com/login')
            req.add_header('Connection', 'keep-alive')
            req.add_header('X-Requested-With', 'XMLHttpRequest')
            req.add_header('Content-Type', 'application/x-www-form-urlencoded')
            
            try:
                category_image = urllib2.urlopen(req).read()
            except:
                category_image = None
            
            if category_image is None or len(category_image) == 0:
                print 'sleep 0.1-----'
                time.sleep(0.1)
            else:
                break
            
        save_file = 'image/category/' + category_name + '.jpg'
        img = open(save_file, "wb")
        img.write(category_image)
        img.close()
        return save_file

    '''获取Figure1首页JSON。pageSize为每页面显示数据条数，skip为页码。目前数据量为16500左右。'''
    def getHomeData(self, pageSize, skip):
        def run(self):
            try:
                url = 'https://app.figure1.com/s/feeds/home?pageSize=' + str(pageSize) + '&skip=' + str(skip)
                req = urllib2.Request(url)
                req.add_header('Host', 'app.figure1.com')
                req.add_header('User-Agent', 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36')
                req.add_header('Referer', 'https://app.figure1.com/login')
                req.add_header('Connection', 'keep-alive')
                req.add_header('X-Requested-With', 'XMLHttpRequest')
                req.add_header('Content-Type', 'application/x-www-form-urlencoded')

                response = urllib2.urlopen(req)

                return {'s': True, 'r': response.read()}
            except:
                print ''
                return {'s': False, 'r': ''}

        re = run(self)

        while not re['s']:
            re = run(self)

        return re['r']

    '''保存figure图片'''
    def saveFigureImage(self, imageid, image_src):
        
        while True:
            url = 'https://app.figure1.com' + image_src
            req = urllib2.Request(url)
            
            print url
            
            req.add_header('Host', 'app.figure1.com')
            req.add_header('User-Agent', 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36')
            req.add_header('Referer', 'https://app.figure1.com/login')
            req.add_header('Connection', 'keep-alive')
            req.add_header('X-Requested-With', 'XMLHttpRequest')
            req.add_header('Content-Type', 'application/x-www-form-urlencoded')
            
            try:
                figure_image = urllib2.urlopen(req).read()
            except:
                figure_image = None
            
            if figure_image is None or len(figure_image) == 0:
                print 'sleep 0.1-----'
                time.sleep(0.1)
            else:
                break
            
        save_file = 'image/figure/' + imageid + '.jpg'
        img = open(save_file, "wb")
        img.write(figure_image)
        img.close()
        
        return True

